import 'package:flutter/material.dart';

class ExpansionPanelDemo extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ExpansionPanelDemo();
  }
}

class _ExpansionPanelDemo extends State<ExpansionPanelDemo> {
  int exPo = -1;

  _ex(int po, bool b){
    print('po = $po , b = $b');

    setState(() {
      if(b) {
        exPo = -1;
      }else {
        exPo = po;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('ExpansionPanel'),
      ),
      body: SingleChildScrollView(
        child: ExpansionPanelList(
          expansionCallback:_ex,
          children: <ExpansionPanel>[
            ExpansionPanel(
              isExpanded: exPo == 0,
                headerBuilder: (context, b) {
                  return ListTile(
                    title: Text('panel1 ${b ? '开' : '关'}'),
                  );
                },
                body: Center(
                  child: Text('ex 1'),
                )),
            ExpansionPanel(
                isExpanded: exPo == 1,
                headerBuilder: (context, b) {
                  return ListTile(
                    title: Text('panel2 ${b ? '开' : '关'}'),
                  );
                },
                body: Center(
                  child: Text('ex 2'),
                )),
          ],
        ),
      ),
    );
  }
}
